#!/bin/bash

#声明目标关联数组
declare -A key_map

key_map["chinatower-smc-inner-service-1.0.jar"]="/app/smc-service/chinatower_smc_inner/chinatower-smc-inner-service-1.0.jar"

#什么备份关联数组
declare -A key_map_bak

key_map_bak["chinatower-smc-inner-service-1.0.jar"]="/app/smc-service/chinatower_smc_inner/bak/"

#声明编译清单关联数组
declare -A key_map_deploy
key_map_deploy["chinatower-smc-inner-service-1.0.jar"]="/app/Develop/CT_SMC_INNER/chinatower/chinatower-smc-inner/chinatower-smc-inner-service/target/chinatower-smc-inner-service-1.0.jar"

if pgrep -f "CT_SMC_INNER.mvn.sh" | grep -v $$ | grep -v "grep" >/dev/null; then
  echo "已经有人在编译中"
else
  requirement_number=$1
  remote_branch_name=$requirement_number
  local_repo_path="/app/Develop/CT_SMC_INNER"

  if [ -d "$local_repo_path" ]; then
    echo "Local repository already exists. Updating branch $remote_branch_name..."
    cd /app/Develop/CT_SMC_INNER/chinatower
    git -c credential.helper=store checkout $remote_branch_name
    git -c credential.helper=store pull
  else
    mkdir -p $local_repo_path
    echo "Cloning the repository for the first time..."
    git -c credential.helper=store clone -b $remote_branch_name http://wx-liuchao76@10.38.124.5:8080/55settlement_center/CT_SMC_INNER.git $local_repo_path
  fi

  if [ $? -eq 0 ]; then
    # 执行编译操作
    echo "***********************"
    echo "* Git pull 成功，执行编译操作 *"
    echo "***********************"
    for i in {1..5}; do
      # 计算进度条长度
      progress=$(($i * 20))
      # 输出进度条和百分比
      printf "即将开始编译: [%-10s] %3d%%\r" $(printf '==%.0s' $(seq 1 $i)) $progress
      sleep 1 # 模拟耗时操作
    done

    cd /app/Develop/CT_SMC_INNER/chinatower
    mvn clean install -U -DskipTests

    for key in "${!key_map[@]}"; do
      echo "Key: $key, Value: ${key_map[$key]}"
      echo "开始更新文件，只做CP操作。没有解压： ${key_map[$key]}"
      if [ -f "${key_map_deploy[$key]}" ]; then
        mkdir -p ${key_map_bak[$key]}
        current_time=$(date +"%Y%m%d%H%M%S")

        cp -T ${key_map[$key]} "${key_map_bak[$key]}$(basename ${key_map[$key]} .jar)_$current_time.jar"
        cp -T ${key_map_deploy[$key]} ${key_map[$key]}
      else
        echo "文件不存在:${key_map_deploy[$key]}"
      fi
    done
  else
    echo "Git pull 失败，无法执行编译操作"
  fi
  echo "scp /app/smc-service/chinatower_smc_inner/chinatower-smc-inner-service-1.0.jar root@10.180.45.177:/app/smc-service/chinatower_smc_inner/ 使用此语句发送到177"
fi
